﻿#include "logging.h"
#include "ui_logging.h"
#include "dealall.h"

Logging::Logging(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Logging)
{
    Mparent = (MainWindow*)parent;
    ui->setupUi(this);
    this->ui->Yesorno->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    this->ui->Yesorno->button(QDialogButtonBox::Ok)->setText(tr("OK"));          //将buttonbox中的ok 变成汉化
    this->ui->Yesorno->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
    this->ui->passward->setEchoMode(QLineEdit::Password);                   //密码方式显示文本
    if(Mparent->name != "")
        this->ui->name->addItem(QString("%1").arg(Mparent->name));
}

Logging::~Logging()
{
    delete ui;
}


void Logging::on_Yesorno_clicked(QAbstractButton *button)
{
    if(ui->Yesorno->button(QDialogButtonBox::Ok) == button)
    {
        if(0)
                //((ui->name->currentText() == "") || (ui->passward->text() == ""))
        {
            QMessageBox::information(this, tr("Warn"), tr("输入不能为空！!"), QMessageBox::Ok);
            return;
        }
        QString name = ui->name->currentText();
        QString passward = ui->passward->text();

        QProgressDialog dialog(tr("Login.."),tr("Cancel"), 0, loginMsec, this);
        dialog.setWindowTitle(tr("progress"));
        dialog.setWindowModality(Qt::WindowModal);
        dialog.show();
        for(int k = 0; k < loginMsec; k++)
        {
            dialog.setValue(k);
            QCoreApplication::processEvents();
            if(dialog.wasCanceled())
            {
                break;
            }
        }
        dialog.setValue(loginMsec);

        My_Sql* mysql = new My_Sql;
        bool ret = mysql->loguser(name, passward);
        //w001 test
        ret=true;
        if(ret == false)
        {
            QMessageBox::information(this, tr("Warn"), tr("账号密码不匹配!"), QMessageBox::Ok);
            this->close();
            Mparent->show();
            return;
        }
        QMessageBox::information(this, tr("Info"), tr("Login OK"), QMessageBox::Ok);
        this->close();
        DealAll* DA = new DealAll(this);
        DA->show();
        return;
    }
    else if(ui->Yesorno->button(QDialogButtonBox::Cancel) == button)
    {
        this->close();
        QProgressDialog dialog(tr("Go to Main UI"),tr("Cancel"), 0, loginMsec, this);
        dialog.setWindowTitle(tr("Progress"));
        dialog.setWindowModality(Qt::WindowModal);
        dialog.show();
        for(int k = 0; k < loginMsec; k++)
        {
            dialog.setValue(k);
            QCoreApplication::processEvents();
            if(dialog.wasCanceled())
            {
                break;
            }
        }
        dialog.setValue(loginMsec);
        Mparent->show();
    }
}
